package cn.itcast.embedding;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.JsonReader;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class CityEmbedding {
    private final VectorStore vectorStore;

    @Value("classpath:rule.json")
    private Resource resource;

    @PostConstruct
    public void init() throws Exception {
        //1.读
        JsonReader jsonReader = new JsonReader(resource);
        //2.切
        List<Document> documentList = jsonReader.get();
        //参数分别是：默认分块大小、最小分块字符数、最小向量化长度（太小的忽略）、最大分块数量、不保留分隔符（\n啥的）
        // TextSplitter textSplitter = new TokenTextSplitter(200, 100, 5, 10000, false);
        // List<Document> list = textSplitter.apply(documentList);
        //3.存
        vectorStore.add(documentList);
        log.info("数据写入向量库成功，数据条数：{}", documentList.size());
    }
}